package com.shen.show

import android.util.Log

interface PlayerListener{
    fun onStatus(status:Int)
    fun onGetFrameYUV(
        width: Int,
        height: Int,
        y: ByteArray,
        u: ByteArray,
        v: ByteArray,
        practicalWidth: Int,
        timeUs: Double
    )

    fun onSizeChange(width:Int,height: Int)
}


public interface GetFrameListener{
    fun onInited(codecName: String?, width: Int, height: Int, csd_0: ByteArray?, csd_1: ByteArray?)
    fun onStarGetFrame()
    fun getFramePacket(dataSize: Int, pts: Double, data: ByteArray?)

    //使用ffmpeg解码得到YUV数据
    fun onGetFrameYUV(
        width: Int,
        height: Int,
        y: ByteArray?,
        u: ByteArray?,
        v: ByteArray?,
        practicalWidth: Int,
        timeUs: Double
    )

    fun onGetFrameYUV2(
        width: Int,
        height: Int,
        yuv: ByteArray?,
        practicalWidth: Int,
        timeUs: Double
    )




}
class NativeLib {


    var mGetFrameListener:GetFrameListener?=null;
    fun onCallYUVToBitmap(
        width: Int,
        height: Int,
        y: ByteArray,
        u: ByteArray,
        v: ByteArray,
        practicalWidth: Int,
        timeUs: Double
    ) {
        mGetFrameListener?.onGetFrameYUV(width, height, y, u, v, practicalWidth, timeUs)
    }

    fun onSizeChange(width:Int,height: Int){
        mPlayerListener?.onSizeChange(width,height);
    }
    fun onStatus(code:Int){
        Log.e(TAG,""+code);
        mPlayerListener?.onStatus(code);
    }

    var player:Long=0L;
    private  val TAG = "NativeLib"


    var mPath=""

    fun setPath(mPath:String){
        this.mPath=mPath
    }
    var mPlayerListener:PlayerListener?=null


    fun onFrameYUV(width: Int, height: Int, y: ByteArray, u:ByteArray, v:ByteArray, practicalWidth: Int,  timeUs: Double){
        mGetFrameListener?.onGetFrameYUV(width,height,y,u,v,practicalWidth,timeUs);
    }


    fun prepare(){
        player=n_prepare(mPath)
    }

    fun initGetFrame(source:String){
        init_frame_by_ffmpeg(source)
    }
    //start decode thread
    fun startGetFrame(){
        n_start_by_ffmpeg();
    }
    /**
     * A native method that is implemented by the 'show' native library,
     * which is packaged with this application.
     */
    external fun n_prepare(path:String):Long;
    external fun n_start(player:Long);
    // init ffmpeg for decode av
    external fun init_frame_by_ffmpeg(source:String);

    external fun n_start_by_ffmpeg();
    external fun get_each_frame();
    fun onGetFrameInitSuccess(
        codecName: String?,
        width: Int,
        height: Int,
        csd_0: ByteArray?,
        csd_1: ByteArray?
    ) {

            mGetFrameListener?.onInited(codecName, width, height, csd_0, csd_1)
//        }
    }

    fun start() {
        n_start(player);
    }

    companion object {


        /***
         * ffmpeg posterize time
         */
        external fun stringFromJNI(): String
        /***
        opengl es
         */



        external fun onSurfaceCreated();

        external fun onSurfaceChanged( width: Int, height: Int);


        external fun onDrawFrame();
        // Used to load the 'show' library on application startup.
        init {
            System.loadLibrary("videoedit")
        }
    }
}